package ${packageName}.service.impl;


import com.shopx.cmm.DTO.PageDTO;
import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.exception.OriginException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.base.service.base.BaseService;
import javax.annotation.Resource;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import com.shopx.dal.redis.CacheServiceFactory;
import ${packageName}.bean.${classInfo.className};
import ${packageName}.model.${classInfo.className}Model;
import ${packageName}.dao.db1.${classInfo.className}Dao;
import ${packageName}.service.I${classInfo.className}Service;

/**
 * TODO
 * @author ${authorName}
 * @className: ${classInfo.classComment}
 * @date ${.now?string('yyyy-MM-dd')}
 */
@Service
@Slf4j
public class ${classInfo.className}ServiceImpl implements I${classInfo.className}Service {

	@Resource
	private ${classInfo.className}Dao ${classInfo.className?uncap_first}Dao;

	@Resource
    private ISysSeqService sysSeqService;

    @Override
    public ${classInfo.className}Model selectByPrimaryKey(${classInfo.className} ${classInfo.className?uncap_first}) {
        if (${classInfo.className?uncap_first}==null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        return ${classInfo.className?uncap_first}Dao.selectByPrimaryKey(${classInfo.className?uncap_first});
    }

    @Override
    @Transactional(value = "db1",rollbackFor = Exception.class)
    public int insertSelective(${classInfo.className} ${classInfo.className?uncap_first}){
        if (${classInfo.className?uncap_first}==null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        <#if classInfo.keyList?exists && classInfo.keyList?size = 1>
            <#list classInfo.keyList as fieldItem >
            String id = sysSeqService.getTabSeq("${classInfo.className}",4,null);
        	${classInfo.className?uncap_first}.set${fieldItem.fieldName?cap_first}(id);
        	</#list>
        </#if>
        int result = ${classInfo.className?uncap_first}Dao.insertSelective(${classInfo.className?uncap_first});
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1",rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(${classInfo.className} ${classInfo.className?uncap_first}) {
        if (${classInfo.className?uncap_first} == null) {
        	throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = ${classInfo.className?uncap_first}Dao.updateByPrimaryKeySelective(${classInfo.className?uncap_first});
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

	@Override
    @Transactional(value = "db1",rollbackFor = Exception.class)
    public int deleteByPrimaryKey(${classInfo.className} ${classInfo.className?uncap_first}){
        if (${classInfo.className?uncap_first}==null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = ${classInfo.className?uncap_first}Dao.deleteByPrimaryKey(${classInfo.className?uncap_first});
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int selectCount(${classInfo.className}Model ${classInfo.className?uncap_first}){
        return ${classInfo.className?uncap_first}Dao.selectCount(${classInfo.className?uncap_first});
    }

    @Override
    public List<${classInfo.className}Model> selectList(${classInfo.className}Model ${classInfo.className?uncap_first}) {
        List<${classInfo.className}Model> list = ${classInfo.className?uncap_first}Dao.selectList(${classInfo.className?uncap_first});
        return list;
    }

    @Override
    public PageDTO<${classInfo.className}Model> getListPage(${classInfo.className}Model ${classInfo.className?uncap_first}) {
      if (${classInfo.className?uncap_first} == null) {
        throw new ShopxException(ResultCodeEnum.PAR_NULL);
      }
      PageDTO<${classInfo.className}Model> pageDTO = new PageDTO<${classInfo.className}Model>();
        pageDTO.setiTotalDisplayRecords(selectCount(${classInfo.className?uncap_first}));
        pageDTO.setData(selectList(${classInfo.className?uncap_first}));
        return pageDTO;
    }

}
